package com.sixth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixth.core.entity.Patient;
import com.sixth.core.vo.R;
import com.sixth.mapper.PatientMapper;
import com.sixth.mapper.RegistrationMapper;
import com.sixth.service.RegistrationService;
import com.sixth.dto.OrderListDto;
import com.sixth.vo.OrderQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: sixth-hospital-parent
 * @description:
 * @author: 李劲锋
 * @create: 2024-08-29 10:45
 **/
@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public R queryAllOrder(OrderQueryVO orderQueryVO) {
        String phone = orderQueryVO.getPhone();
        String orderState = orderQueryVO.getOrderState();

        if (phone == null) {
            return R.error("未获取到信息");
        }

        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        Patient patient = patientMapper.selectOne(queryWrapper);

        if (patient == null) {
            return R.error("未找到患者信息");
        }

        String patientId = patient.getPatientId();

        // 将 orderState 为 "4" 的情况传递为 null
        String queryOrderState = "4".equals(orderState) ? null : orderState;

        List<OrderListDto> orderListDto = registrationMapper.selectListOrder(patientId, queryOrderState);

        return R.ok(orderListDto);
    }


}
